﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace PMS.Controllers.BD
{
    public class GradeTypeController : Controller
    {
        public ActionResult GradeTypeList()
        {
            return View();
        }
        //查询
        public ActionResult List(BD_GradeType GradeType)
        {
            List<SearchCondition> listSearch = new List<SearchCondition>();
            if (Request.Form["searchData"] != null)
            {
                string searchData = Request.Form["searchData"];
                listSearch = ModelTools.GetSearchContions(searchData);
            }
            listSearch.Add(new SearchCondition() { Field = "DelState", Operator = "=", Value = "0" });
            Expression<Func<BD_GradeType, bool>> exp = SearchExpression.FindByGroup<BD_GradeType>(GradeType, listSearch);
            string OrderId = OperateContext.Sort == null ? "Id" : OperateContext.Sort;
            int count = 0;
            var list = BLLSession.Current.BD_GradeTypeBLL.GetPagedList(OperateContext.Page, OperateContext.Rows, exp, t => t.ID, true);
            count = BLLSession.Current.BD_GradeTypeBLL.GetCount(exp);
            return Json(new EasyuiDataPager() { rows = list, total = count });
        }
        //添加
        public ActionResult AddGradeType(BD_GradeType GT)
        {
            var list1 = DALSession.Current.BD_GradeTypeDAL.GetListBy(d => d.GradeType == GT.GradeType && d.DelState == "0");
            if (list1.Count == 0)
            {
                GT.GradeIndex = (Convert.ToInt32(GT.GradeIndex)).ToString("00");
                GT.EditTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                GT.EditPerName = OperateContext.sessUserInfo.Name;
                GT.EditPerID = OperateContext.sessUserInfo.Id;
                GT.DelState = "0";
                BLLSession.Current.BD_GradeTypeBLL.Add(GT);
                return Json(new Message() { msg = "保存成功", state = "success" });
            }
            else
            {
                return Json(new Message() { msg = "数据已存在！", state = "warning" });
            }
        }
        //修改
        public ActionResult EditGradeType(BD_GradeType GT)
        {
            GT.GradeIndex = (Convert.ToInt32(GT.GradeIndex)).ToString("00");
            GT.EditTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            GT.EditPerName = OperateContext.sessUserInfo.Name;
            GT.EditPerID = OperateContext.sessUserInfo.Id;
            BLLSession.Current.BD_GradeTypeBLL.ModifyBy(GT, t => t.ID == GT.ID, "GradeType", "GradeIndex", "EditPerID", "EditPerName", "EditTime", "Note", "DelState");
            return Json(new Message() { msg = "保存成功", state = "success" });
        }
        //删除
        public ActionResult DelGradeType(int Id)
        {
            //BLLSession.Current.BD_GradeTypeBLL.DelBy(d => d.ID == Id);//直接删除
            BD_GradeType BD_GradeType1 = DALSession.Current.BD_GradeTypeDAL.GetListBy(d => d.ID == Id).FirstOrDefault();
            BD_GradeType1.EditTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            BD_GradeType1.EditPerName = OperateContext.sessUserInfo.Name;
            BD_GradeType1.EditPerID = OperateContext.sessUserInfo.Id;
            BD_GradeType1.DelState = "1";
            BLLSession.Current.BD_GradeTypeBLL.ModifyBy(BD_GradeType1, t => t.ID == BD_GradeType1.ID, "GradeType", "EditPerID", "EditPerName", "EditTime", "Note", "DelState");
            return Json(new Message() { msg = "删除成功", state = "success" });
        }
        //获取年级基本信息
        public void GetGradeTypeInfo()
        {
            Response.Write(JsonHelper.toJson(BLLSession.Current.BD_GradeTypeBLL.GetListBy(p => p.DelState == "0").Select(p => new { value = p.ID, text = p.GradeType })));
        }
    }
}
